﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Models
{
	public class ExtObject : Object
	{
		public override Class Type
		{
			get
			{
				return base.Type;
			}
			set
			{
				base.Type = value;
				foreach (Config config in ((ExtClass)value).Configs.Where(c => c.DefaultValue != null))
				{
					if (this.Configs[config.Name] == null)
						this.Configs[config.Name] = config.DefaultValue;
				}
			}
		}

		private ConfigDictionary _Configs;
		public ConfigDictionary Configs
		{
			get
			{
				if (this._Configs == null)
				{
					this._Configs = new ConfigDictionary();
					//foreach (Config config in ((ExtClass)this.Type).Configs.Where(c => Config.RequiredConfigs.Contains(c.Name)))
					//{
					//    CustomCode o = new CustomCode();
					//    o.ValueCode = "[]";
					//    //Class type = config.Types[0];
					//    //object value = null;
					//    //if (type == Class.String)
					//    //    value = "";
					//    //else if (type == Class.Number)
					//    //    value = 0;
					//    //else if (type == Class.Boolean)
					//    //    value = false;
					//    //else if (type == Class.Date)
					//    //    value = System.DateTime.Now;

					//    //Object o;
					//    //if (value == null)
					//    //{
					//    //    if (type is ArrayClass)
					//    //    {
					//    //        o = new ArrayObject();
					//    //        o.Type = type;
					//    //    }
					//    //    if (type == Class.Object)
					//    //    {
					//    //        o = new CustomCode();
					//    //        o.Type = type;
					//    //    }
					//    //    else
					//    //    {
					//    //        o = new ExtObject();
					//    //        o.Type = type;
					//    //    }
					//    //}
					//    //else
					//    //{
					//    //    o = new NativeObject();
					//    //    o.Type = type;
					//    //    ((NativeObject)o).Value = value;
					//    //}
					//    this.Configs[config.Name] = o;
					//}
				}
				return this._Configs;
			}
		}

		//public object GetConfigValue(string name)
		//{
		//    ConfigValue config = this.Configs.FirstOrDefault(c => c.Name == name);
		//    if (config == null)
		//        return null;
		//    return config.Value;
		//}

		//public void SetConfigValue(string name, Class type, object value)
		//{
		//    ConfigValue config = this.Configs.FirstOrDefault(c => c.Name == name);
		//    if (config == null)
		//    {
		//        config = new ConfigValue();
		//        config.Name = name;
		//    }
		//    config.Type = type;
		//    config.Value = value;
		//}

		private System.Collections.Generic.List<EventValue> _Events;
		public System.Collections.Generic.List<EventValue> Events
		{
			get
			{
				if (this._Events == null)
					this._Events = new List<EventValue>();
				return this._Events;
			}
		}

		public override string ConfigsString
		{
			get
			{

				return string.Format("new {0}({{{1}}})", this.Type.Name, string.Join(",", this.Configs.Select(c => c.Key + ":" + c.Value.ConfigsString).ToArray()));
			}
		}

		public ExtObject()
		{
		}

		#region ISerializable 成员

		public ExtObject(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
			: base(info, context)
		{
			this._Configs = (ConfigDictionary)info.GetValue("Configs", typeof(ConfigDictionary));
			this._Events = (System.Collections.Generic.List<EventValue>)info.GetValue("Events", typeof(System.Collections.Generic.List<EventValue>));
		}

		public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
		{
			info.AddValue("Configs", this.Configs);
			info.AddValue("Events", this.Events);
		}

		#endregion
	}
}
